package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.frontVo.CourseQueryVo;
import com.atguigu.eduservice.entity.frontVo.CourseWebVo;
import com.atguigu.eduservice.entity.vo.CourseInfoForm;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author lhl
 * @since 2022-07-04
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    EduCourseDescriptionService descriptionService;
    @Resource
    EduVideoService videoService;
    // 章节
    @Resource
    EduChapterService chapterService;
    @Override
    public String saveCourse(CourseInfoForm course) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(course,eduCourse);
        baseMapper.insert(eduCourse);
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(course.getDescription());
        eduCourseDescription.setId(eduCourse.getId());
        descriptionService.save(eduCourseDescription);
        return eduCourse.getId();
    }

    @Override
    public CourseInfoForm getCourseById(String courseID) {
        // 首先根据id查询
        EduCourse eduCourse = baseMapper.selectById(courseID);
        // 包装成CourseInfoForm对象
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        // 复制eduCourse属性到courseInfoForm对象中
        BeanUtils.copyProperties(eduCourse,courseInfoForm);
        // 查询描述表
        EduCourseDescription byId = descriptionService.getById(courseID);
        courseInfoForm.setDescription(byId.getDescription());
        return courseInfoForm;
    }

    @Override
    public String updateSaveCourse(CourseInfoForm course) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(course,eduCourse);
        System.out.println("属性为:"+course);
        baseMapper.updateById(eduCourse);
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(course.getId());
        eduCourseDescription.setDescription(course.getDescription());
        return course.getId();
    }

    @Override
    public CoursePublishVo getCoursePublishVoById(String id) {
        return baseMapper.selectCoursePublishVoById(id);
    }

    @Override
    public void publishCourseById(String id) {
        EduCourse course = new EduCourse();
        course.setId(id);
        course.setStatus("Normal");
        this.updateById(course);
    }

    @Override
    public void removeCourseById(String id) {
        // 首先删除video记录
        videoService.removeViById(id);
        // 删除chapter记录
        chapterService.removeChapById(id);
        // 删除描述
        descriptionService.removeById(id);
        // 删除Course记录
        this.removeById(id);
    }

    @Override
    public Map<String, Object> getCourseList(Page<EduCourse> eduCoursePage, CourseQueryVo current) {
        QueryWrapper<EduCourse>eduCourseQu = new QueryWrapper<>();
        String subjectId = current.getSubjectId();
        String subjectParentId = current.getSubjectParentId();
        // 判断是否有值  二级分类id
        if(!StringUtils.isEmpty(subjectId)){
            eduCourseQu.eq("subject_id",subjectId);
        }
        // 一级分类id
        if(!StringUtils.isEmpty(subjectParentId)){
            eduCourseQu.eq("subject_parent_id",subjectParentId);
        }
        if (!StringUtils.isEmpty(current.getBuyCountSort())) {
            eduCourseQu.orderByDesc("buy_count");
        }

        if (!StringUtils.isEmpty(current.getGmtCreateSort())) {
            eduCourseQu.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(current.getPriceSort())) {
            eduCourseQu.orderByDesc("price");
        }
        baseMapper.selectPage(eduCoursePage, eduCourseQu);
        List<EduCourse> records = eduCoursePage.getRecords();
        long currentNew = eduCoursePage.getCurrent();
        long pages = eduCoursePage.getPages();
        long size = eduCoursePage.getSize();
        long total = eduCoursePage.getTotal();
        boolean hasNext = eduCoursePage.hasNext();
        boolean hasPrevious = eduCoursePage.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", currentNew);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    @Override
    public CourseWebVo selectInfoWebById(String id) {
        CourseWebVo courseWebVo = baseMapper.selectInfoWebById(id);
        return courseWebVo;
    }
}
